package nina.yiyou.salary.process.salary

import groovy.transform.PackageScope

@PackageScope
class SalaryCalculator {

  interface Result {

    double probationTotal()

    double fullTotal()

    double probationActual()

    double fullActual()
  }

  SalaryCalculator(Map attendLine, Map payMap) {
    _attendLine = attendLine
    _payMap = payMap
  }

  Result calc() {
    def payLine = _payMap[_attendLine['工号']] as Map
    assert payLine, "薪资表缺人：${_attendLine}"

    def totalTime = _attendLine['应出勤'] as double
    Pair wage = getWage(payLine, totalTime)

    def getHour = { String k -> _attendLine[k] as double }
    return new Result() {
      @Override
      double probationTotal() {
        return wage.probation() * getHour('试用应出勤')
      }

      @Override
      double fullTotal() {
        return wage.full() * getHour('转正应出勤')
      }

      @Override
      double probationActual() {
        return wage.probation() * getHour('试用实际出勤')
      }

      @Override
      double fullActual() {
        return wage.full() * getHour('转正实际出勤')
      }
    }
  }

  private Pair getWage(Map line, double time) {
    def wage = { String moneyKey ->
      def money = line[moneyKey]
      assert money != null, "薪资表找不到列：<${moneyKey}>"
      return (money instanceof Number) ? (money / time) : 0.0
    }

    return new Pair() {
      @Override
      double probation() {
        return wage('试用期工资')
      }

      @Override
      double full() {
        return wage('转正工资')
      }
    }
  }

  private interface Pair {

    double probation()

    double full()
  }

  /**
   * @see nina.yiyou.salary.input.info.AttendExcelReader#combine
   * @see nina.yiyou.salary.input.info.AttendExcelV2Reader#COL
   */
  private final Map<String, ?> _attendLine

  private final Map _payMap
}
